package cn.test.rabbitmq.normal;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author hanchao
 * @date 2017/6/30 14:35
 */
public class FanoutProducer {
    private static final String QUEUE_NAME1 = "test-queue-fa1";
    private static final String QUEUE_NAME2 = "test-queue-fa2";
    private static final String FANOUT_EXCHANGE= "test-exchange-fanout";
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.1.163");
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("han");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        String message = "Hello RabbitMQ";

        channel.queueDeclare(QUEUE_NAME1, false, false, false, null);
        channel.queueDeclare(QUEUE_NAME2, false, false, false, null);

        channel.exchangeDeclare(FANOUT_EXCHANGE, BuiltinExchangeType.FANOUT.getType());

        channel.queueBind(QUEUE_NAME1,FANOUT_EXCHANGE,"test-queue-fa");
        channel.queueBind(QUEUE_NAME2,FANOUT_EXCHANGE,"test-queue-fa");

        for (int i = 0; i < 10; i++) {
            channel.basicPublish(FANOUT_EXCHANGE, "test-queue-fa", null, (message+i).getBytes("UTF-8"));
        }
        //关闭通道和连接
        channel.close();
        connection.close();
    }
}
